package com.greenantmall.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.avos.avoscloud.AVACL;
import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVRelation;
import com.greenantmall.utils.iLog;

@AVClassName(AntAVObject.CLASS_ORDER)
public class Order extends AntAVObject {
	public static final String TAG = "Order";// 订单id

	//// // 0未配送，1已配送，2已配送至自提点，3已签收，4-待付款，5-待收货，6已收款，7退货中
	public final static int ORDER_TYPE_ALL = -1;
	// public final static int ORDER_TYPE_UNSEND = 0;//0 -待发货
	// public final static int ORDER_TYPE_SENDED = 1;//1-已发货
	// public final static int ORDER_TYPE_SEND_SELF = 2;//2-已送至自提点
	// public final static int ORDER_TYPE_RECEIVED = 3;//3-已签收
	//
	// public final static int ORDER_TYPE_UNPAY = 4;//4-待付款
	// public final static int ORDER_TYPE__WAIT_FOR_SIGN = 5;//5-待收货
	// public final static int ORDER_TYPE_PAYED = 6;//6-退款中
	// public final static int ORDER_TYPE_RETURN = 7;//7-已退款
	//
	// public final static int ORDER_TYPE_FINISHED = 8;//8-已完成
	// public final static int ORDER_TYPE_WEIGHED = 9;//9-已称重
	// public final static int ORDER_TYPE_REFUND_ACCEPT = 10;//10-同意退款
	// public final static int ORDER_TYPE_CANCELED = 11;//11-已取消
	// public final static int ORDER_TYPE_CLOSED = 12;//12-已关闭

	public static final String ATTR_ORDERID = "orderId";// 订单id
	public static final String ATTR_CUSTOMID = "customId";// 下单账号，用户ID
	public static final String ATTR_STOREID = "storeId";// 商家账号
	public static final String ATTR_UPTOWNS = "uptowns";// 服务区
	public static final String ATTR_PAYWAY = "payWay";// 支付方式：货到付款，微信支付，支付宝支付
	public static final String ATTR_PAYTIME = "payTime";// 支付时间
	public static final String ATTR_DELIVERYTIME = "deliveryTime";// 即时配送，xx（如15:00）送达，
	public static final String ATTR_DELIVERYCOST = "deliveryCost";
	public static final String ATTR_DELIVERYWAY = "deliveryWay";// 配送方式：自提、送货上门；默认选择自提，自提点为同一小区的CVS
	public static final String ATTR_STATUSCODE = "statusCode";// 订单状态：0代配送，1已配送，2已配送至自提点，3已签收，4已关闭，5待付款，6代收款，7退货中
	public static final String ATTR_CONSIGNEE = "consignee";// 收件人姓名
	public static final String ATTR_PHONE = "phone";// 收件人电话
	public static final String ATTR_ADDRESS = "address";// 收件人地址
	public static final String ATTR_CUSTOMREMARK = "customRemark";// 顾客备注
	public static final String ATTR_FAVORABLECODE = "favorableCode";// 优惠码：0-无优惠（默认），其他值待定；
	public static final String ATTR_TOTALCOST = "totalCost";// 订单总价
	public static final String ATTR_COMMENT = "comment";// 商家备注
	public static final String ATTR_ORDERITEMS = "orderItems";// 订单商品信息
	public static final String ATTR_ORDERTYPE = "orderType";// 订单类型
	public static final String ATTR_SECTIONNUM = "sectionNum";// 区域类型
	public static final String ATTR_PICKUPSTATIONID = "pickUpStationId";// 自提点

	// 商店类型：
	/**
	 * 1.购物车是分为2中类型的，对于独立店铺（非SP类型的店铺）的页面，显示的是此店铺的购物车，下订单时，
	 * 商品全是当前店铺的，赋值OrderType为1，同时赋值storeId；
	 * 2.对于SP的店铺，同一个sectionNum中的SP店铺的商品都可以在一个订单中，此时，赋
	 * 值OrderType为0，同时赋值sectionNum
	 */
	public static final int STORE_TYPE_SP = 0;
	public static final int STORE_TYPE_SINGLE = 1;

	public static final String ATTR_SIGNEDTIME = "signTime";// 签收时间
	public static final String ATTR_SENDTIME = "sendTime";// 发货时间
	public static final String ATTR_HASPAY = "haspay";// 1表示已支付
	public static final String ATTR_FAVORABLECOST = "favorableCost";//优惠金额

	private List<OrderItem> orderItems;

	public Order() {
		super();
		setAccessCtrl();
	}

	public Order(String str) {
		super(str);
		setAccessCtrl();
	}

	public String getOrderId() {
		return this.getString(ATTR_ORDERID);
	}

	public void setOrderId(String descrip) {
		this.put(ATTR_ORDERID, descrip);
	}

	public String getPickUpStationId() {
		return this.getString(ATTR_PICKUPSTATIONID);
	}

	public void setPickUpStationId(String descrip) {
		this.put(ATTR_PICKUPSTATIONID, descrip);
	}

	public String getCustomId() {
		return this.getString(ATTR_CUSTOMID);
	}

	public void setCustomId(String customId) {
		this.put(ATTR_CUSTOMID, customId);
	}

	public String getStoreId() {
		return this.getString(ATTR_STOREID);
	}

	public void setStoreId(String storeId) {
		this.put(ATTR_STOREID, storeId);
	}

	public AVRelation<ServerSection> getUptowns() {
		return this.getRelation(ATTR_UPTOWNS);
	}

	public String getPayWay() {
		return this.getString(ATTR_PAYWAY);
	}

	public void setPayWay(String storeId) {
		this.put(ATTR_PAYWAY, storeId);
	}

	public Date getPayTime() {
		return this.getDate(ATTR_PAYTIME);
	}

	public void setPayTime(Date payTime) {
		this.put(ATTR_PAYTIME, payTime);
	}

	public Date getDeliveryTime() {
		return this.getDate(ATTR_DELIVERYTIME);
	}

	public void setDeliveryTime(Date deliveryTime) {
		this.put(ATTR_DELIVERYTIME, deliveryTime);
	}

	public String getDeliveryWay() {
		return this.getString(ATTR_DELIVERYWAY);
	}

	public void setDeliveryWay(String deliveryWay) {
		this.put(ATTR_DELIVERYWAY, deliveryWay);
	}

	public double getDeliveryCost() {
		return this.getDouble(ATTR_DELIVERYCOST);
	}

	public void setDeliveryCost(double deliveryCost) {
		this.put(ATTR_DELIVERYCOST, deliveryCost);
	}

	public int getStatusCode() {
		return this.getInt(ATTR_STATUSCODE);
	}

	public void setStatusCode(int statusCode) {
		this.put(ATTR_STATUSCODE, statusCode);
	}

	public String getConsignee() {
		return this.getString(ATTR_CONSIGNEE);
	}

	public void setConsignee(String consignee) {
		this.put(ATTR_CONSIGNEE, consignee);
	}

	public String getPhone() {
		return this.getString(ATTR_PHONE);
	}

	public void setPhone(String phone) {
		this.put(ATTR_PHONE, phone);
	}

	public String getAddress() {
		return this.getString(ATTR_ADDRESS);
	}

	public void setAddress(String address) {
		this.put(ATTR_ADDRESS, address);
	}

	public String getCustomRemark() {
		return this.getString(ATTR_CUSTOMREMARK);
	}

	public void setCustomRemark(String customRemark) {
		this.put(ATTR_CUSTOMREMARK, customRemark);
	}

	public int getFavorableCode() {
		return this.getInt(ATTR_FAVORABLECODE);
	}

	public void setFavorableCode(int favorableCode) {
		this.put(ATTR_FAVORABLECODE, favorableCode);
	}

	public double getTotalCost() {
		return this.getDouble(ATTR_TOTALCOST);
	}

	public void setTotalCost(double totalCost) {
		this.put(ATTR_TOTALCOST, totalCost);
	}

	public String getComment() {
		return this.getString(ATTR_COMMENT);
	}

	public void setComment(String comment) {
		this.put(ATTR_COMMENT, comment);
	}

	public int getOrderType() {
		return this.getInt(ATTR_ORDERTYPE);
	}

	public void setOrderType(int comment) {
		this.put(ATTR_ORDERTYPE, comment);
	}

	public int getSectionNum() {
		return this.getInt(ATTR_SECTIONNUM);
	}

	public void setSectionNum(int comment) {
		this.put(ATTR_SECTIONNUM, comment);
	}

	public void setOrderItems(List<OrderItem> comment) {
		this.orderItems = comment;
	}

	public List<OrderItem> getOrderItems() {
		if (orderItems == null) {
			orderItems = new ArrayList<OrderItem>();
		}
		orderItems.clear();
		String list = this.getString(ATTR_ORDERITEMS);
		if (list != null) {

			try {
				JSONArray array = new JSONArray(list);
				iLog.d(TAG, "getMyOrderByType array:" + list + " toJSONString:" + array.toString() + " this:"
						+ this.toJSONObject().toString());
				for (int i = 0; i < array.length(); i++) {
					JSONObject object = array.getJSONObject(i);
					OrderItem orderItem = new OrderItem();
					orderItem.setCost(object.optDouble(OrderItem.KEY_COST));
					orderItem.setGoodsId(object.optString(OrderItem.KEY_GOODSID));
					orderItem.setPicUrl(object.optString(OrderItem.KEY_PICURL));
					orderItem.setQuantity(object.optDouble(OrderItem.KEY_QUANTITY));
					orderItem.setName(object.optString(OrderItem.KEY_NAME));

					orderItem.setPrice(object.optDouble(OrderItem.KEY_PRICE));
					orderItem.setUnit(object.optString(OrderItem.KEY_UNIT));
					orderItem.setWeighUnit(object.optString(OrderItem.KEY_WEIGHUNIT));
					orderItem.setWeighAble(object.optBoolean(OrderItem.KEY_WEIGHABLE));
					orderItem.setWeight(object.optDouble(OrderItem.KEY_weight));

					orderItems.add(orderItem);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

		}
		return orderItems;
	}

	public Order getInstance() {
		return this;
	}

	private int setAccessCtrl() {

		acl = new AVACL();
		acl.setPublicReadAccess(true);// �˴����õ��������˵Ŀɶ�Ȩ��
		acl.setPublicWriteAccess(true);
		this.setACL(acl);
		return 0;
	}

	public boolean isSp() {
		if (this.getOrderType() == STORE_TYPE_SINGLE) {
			return false;
		}
		return true;
	}

	public boolean isHaveWeight() {
		List<OrderItem> items = getOrderItems();
		if (items != null && items.size() > 0) {
			for (int i = 0; i < items.size(); i++) {
				if (items.get(i).isWeighAble()) {
					return true;
				}
			}

		}
		return false;
	}

	public boolean isWeighted() {
		if (isHaveWeight()) {
			List<OrderItem> items = getOrderItems();
			if (items != null && items.size() > 0) {
				for (int i = 0; i < items.size(); i++) {
					// 未称重
					if (items.get(i).isWeighAble()) {
						if (items.get(i).getWeight() < 0.001) {
							return false;
						}
					}
				}

			}
		}
		return true;
	}

	public Date getSignedTime() {
		return this.getDate(ATTR_SIGNEDTIME);
	}

	public void setSignedTime(Date payTime) {
		this.put(ATTR_SIGNEDTIME, payTime);
	}

	public Date getSendTime() {
		return this.getDate(ATTR_SENDTIME);
	}

	public void setSendTime(Date payTime) {
		this.put(ATTR_SENDTIME, payTime);
	}

	public boolean isPayed() {
		return this.getInt(ATTR_HASPAY) == 1;
	}

	public void setPayed(boolean payed) {
		this.put(ATTR_HASPAY, payed ? 1 : 0);
	}

	public double getFavorableCost() {
		return this.getDouble(ATTR_FAVORABLECOST);
	}

	public void setFavorableCost(double cost) {
		this.put(ATTR_FAVORABLECOST, cost);
	}
}
